package com.baiytfp.hf.HFDataExchange.action.dao.impl;

import com.baiytfp.hf.HFDataExchange.action.dao.FuncDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class OracleFuncDAOImpl extends FuncDAOImpl {

	@Override
	public List<Map> listFuncForRight(String treeType, String objCode, String objType) throws Exception {
		StringBuffer sWhere = new StringBuffer();
		List params = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.*, attr.func_name,   ");
		sql.append("  nvl(attr.func_code,'') func_code, nvl(a.func_type,'2') func_type,  ");
		sql.append(" nvl(a.is_auth,0) is_auth ,nvl(a.is_disabled,0) is_disabled,  nvl(a.id,'0') as is_edit ");
		sql.append(" from ");
		sql.append(TableName.TREE + " t1 left join ");
		sql.append(TableName.FUNC + " attr on t1.id=attr.tree_id ");
		sql.append(" left join ( select * from "+TableName.AUTH + " c where ")
		.append(" ( c.obj_type=? or c.obj_type is null ) and ( c.obj_code like ? or c.obj_code is null ) ")
		.append(" )a on attr.tree_id=a.func_id ");
		
		params.add(objType);
		params.add(objCode);
		
		if (sWhere.length() > 0) {
			sWhere.append(" and ");
		}
		sWhere.append(" TREE_TYPE=? and (IS_OPEN=1 OR IS_OPEN IS NULL) ");
		params.add(treeType);

		
		
		// String orderBy =
		// " order by node_deep desc, parent_seq_code, node_order asc "; //
		// 先把父结点取出来
		String orderBy = " order by node_deep asc, parent_tree_code, node_order asc "; // 先把父结点取出来

		List<Map> list = HFDBHelper.querySql(sql + " where " + sWhere.toString() + orderBy, Map.class, params.toArray());

		return list;
	}
	
	
	
}
